﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.HVAC.PipeSetting;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class ClassDuctSystemTypeManage : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			try
			{
				UIApplication application = commandData.Application;
				Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
				Document document = application.ActiveUIDocument.Document;
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Autodesk.Revit.DB.View activeView = document.ActiveView;
				Selection selection = activeUIDocument.Selection;
				transaction = new Transaction(document, "水管设置管理");
				transaction.Start();
				PipeSettingManager.Execute(document);
				transaction.Commit();
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(transaction, false);
				return Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				this.EndTransation(transaction, true);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void EndTransation(Transaction tran, bool beSure)
		{
			if (tran != null)
			{
				TransactionStatus status = tran.GetStatus();
                if ((int)status == 1)
				{
					if (beSure)
					{
						tran.Commit();
						return;
					}
					tran.RollBack();
					return;
				}
                else if ((int)status == 5)
				{
					tran.RollBack();
				}
			}
		}

		public List<PipingSystemType> Get_PipingSystemType(Autodesk.Revit.DB.Document document)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(document).OfClass(typeof(PipingSystemType)).ToElements();
			List<PipingSystemType> list = new List<PipingSystemType>();
			foreach (Element element in enumerable)
			{
				PipingSystemType item = element as PipingSystemType;
				list.Add(item);
			}
			return list;
		}

		public List<MechanicalSystemType> Get_MechanicalSystemType(Autodesk.Revit.DB.Document document)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(document).OfClass(typeof(MechanicalSystemType)).ToElements();
			List<MechanicalSystemType> list = new List<MechanicalSystemType>();
			foreach (Element element in enumerable)
			{
				MechanicalSystemType item = element as MechanicalSystemType;
				list.Add(item);
			}
			return list;
		}
	}
}
